﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eassets.Layered.Domain.Global;
using Eassets.Service.Global;

namespace Eassets.UI.GlobalReference
{
    public partial class AuthorizationGroupsForm : System.Web.UI.Page
    {
        TextBox textbox;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBindToGridViewAuthorization();
            }
        }

        protected void TextBoxTemplateValidation()
        {

            foreach (GridViewRow gridViewRow in gridViewAuthorizationGroup.Rows)
            {
                textbox = new TextBox();
                textbox = (TextBox)gridViewRow.Cells[2].FindControl("textBoxMaxNoMember");
                textbox.Attributes.Add("onkeypress", "javascript:return handleNumbersOnly(event,this)");
            }
        }




        //protected void FillData()
        //{


        //    gridViewAuthorizationGroup.DataSource = CreateDataSourceMappedProperties();
        //    gridViewAuthorizationGroup.DataBind();


        //}
        protected void gridViewAuthorizationGroup_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 1)
            {

                e.Row.Cells[2].Width = Unit.Percentage(40);
                if (e.Row.Cells.Count > 1)
                {
                    e.Row.Cells[1].Visible = false;
                }

            }
        }
        private void DataBindToGridViewAuthorization()
        {
            AuthorizationGroup authorizationGroup = new AuthorizationGroup();
            AuthorizationGroupService authorizationGroupsService = new AuthorizationGroupService();

            gridViewAuthorizationGroup.DataSource = authorizationGroupsService.GetAllAuthorizationGroup();
            gridViewAuthorizationGroup.DataBind();

        }
        protected void gridViewAuthorizationGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = gridViewAuthorizationGroup.SelectedRow;
            if (!(String.IsNullOrEmpty(row.Cells[1].Text)))
            {
                textBoxAuthorizationGroupCodeAdd.Text = row.Cells[1].Text;
            }

            textBoxAuthorizationGroupCodeAdd.Text = row.Cells[2].Text;
            textBoxAuthorizationGroupDescriptionAdd.Text = row.Cells[3].Text;

            //CreateDataTable(Convert.ToInt32(textBoxAuthorizationGroupCodeAdd.Text));


            //accordionInputs.SelectedIndex = 1;
            gridViewAuthorizationGroup.Enabled = true;

        }

        ////protected void gridViewAuthorizationGroup_RowCreated(object sender, GridViewRowEventArgs e)
        ////{

        ////    if (e.Row.Cells.Count > 1)
        ////    {
        ////        e.Row.Cells[1].Visible = false;
        ////    }

        ////}


        //ICollection CreateDataSourceMappedProperties()
        //{

        //    // Create sample data for the DataList control.
        //    DataTable dt = new DataTable();
        //    DataRow dr;

        //    // Define the columns of the table.
        //    dt.Columns.Add(new DataColumn("Business Unit", typeof(String)));
        //    dt.Columns.Add(new DataColumn("Position", typeof(String)));
        //    dt.Columns.Add(new DataColumn("MaxNoMember", typeof(Int32)));

        //    bool aa = true;
        //    // Populate the table with sample values.
        //    //for (int i = 0; i < 3; i++)
        //    //{
        //    dr = dt.NewRow();

        //    dr[0] = "Head Office";
        //    dr[1] = "Branch Manager";
        //    dr[2] = 1;

        //    dt.Rows.Add(dr);
        //    dr = dt.NewRow();

        //    dr[0] = "Head Office";
        //    dr[1] = "Branch Head";
        //    dr[2] = 2;

        //    dt.Rows.Add(dr);
        //    dr = dt.NewRow();

        //    dr[0] = "Head Office";
        //    dr[1] = "Branch Officer";
        //    dr[2] = 3;

        //    dt.Rows.Add(dr);
        //    //}

        //    DataView dv = new DataView(dt);
        //    return dv;
        //}

        //protected void CreateDataTable(long authorizationGroupId)
        //{

        //    AuthorizationGroupLevelService authorizationGroupLevelService = new AuthorizationGroupLevelService();
        //    Collection<AuthorizationGroupLevel> authorizationGroupLevels = authorizationGroupLevelService.GetAllAuthorizationGroupLevelByAuthorizationGroupId(authorizationGroupId);
        //    DataRow dr;
        //    DataTable dataTable = new DataTable();

        //    dataTable.Columns.Add(new DataColumn("BUSINESSUNIT", typeof(String)));
        //    dataTable.Columns.Add(new DataColumn("BUSINESSUNITID", typeof(Int64)));
        //    ////
        //    dataTable.Columns.Add(new DataColumn("SELECTEDNODEBULIST", typeof(Int64)));
        //    dataTable.Columns.Add(new DataColumn("POSITION", typeof(String)));
        //    dataTable.Columns.Add(new DataColumn("CURRENTPOSITIONID", typeof(Int64)));
        //    dataTable.Columns.Add(new DataColumn("MAXNOOFMEMBERS", typeof(Int32)));
        //    dataTable.Columns.Add(new DataColumn("AuthorizationGroupLevelId", typeof(Int64)));
        //    //dr[0] = "1";
        //    foreach (AuthorizationGroupLevel authorizationGroupLevel in authorizationGroupLevels)
        //    {
        //        dr = dataTable.NewRow();
        //        dr["BUSINESSUNIT"] = authorizationGroupLevel.BusinessUnitList.description;
        //        dr["BUSINESSUNITID"] = authorizationGroupLevel.BusinessUnitList.BusinessUnitListID;
        //        ////
        //        dr["SELECTEDNODEBULIST"] = authorizationGroupLevel.BusinessUnitList.BusinessUnitListID;
        //        if (authorizationGroupLevel.CompanyPositionStructure.PositionHierarchy != null)
        //        {
        //            dr["POSITION"] = authorizationGroupLevel.CompanyPositionStructure.PositionHierarchy.LeadershipPosition.Description;
        //        }
        //        else
        //        {
        //            dr["POSITION"] = "";
        //        }

        //        dr["CURRENTPOSITIONID"] = authorizationGroupLevel.CompanyPositionStructure.CompanyPositionStructureId;
        //        dr["MAXNOOFMEMBERS"] = authorizationGroupLevel.NoOfMember;
        //        dr["AuthorizationGroupLevelId"] = authorizationGroupLevel.AuthorizationGroupLevelId;
        //        dataTable.Rows.InsertAt(dr, 0);
        //    }

        //    ViewState["griddatasource"] = dataTable;
        //    gridViewAuthorizationGroupLevel.DataSource = dataTable;
        //    gridViewAuthorizationGroupLevel.DataBind();

        //}

        //protected void buttonNewRecord_Click(object sender, EventArgs e)
        //{
        //    //CreateDataTable();
        //    DataRow dr;

        //    dr = ((DataTable)ViewState["griddatasource"]).NewRow();

        //    dr["BUSINESSUNIT"] = "";
        //    dr["BUSINESSUNITID"] = 0;
        //    ////
        //    dr["SELECTEDNODEBULIST"] = 0;
        //    dr["POSITION"] = "";
        //    dr["CURRENTPOSITIONID"] = 0;
        //    dr["MAXNOOFMEMBERS"] = 0;
        //    dr["AuthorizationGroupLevelId"] = 0;
        //    ((DataTable)ViewState["griddatasource"]).Rows.Add(dr);

        //    gridViewAuthorizationGroupLevel.DataSource = ((DataTable)ViewState["griddatasource"]);
        //    gridViewAuthorizationGroupLevel.DataBind();
        //}

        //protected void gridViewAuthorizationGroupLevel_RowDataBound(object sender, GridViewRowEventArgs e)
        //{
        //    ((TextBox)e.Row.Cells[2].FindControl("textBoxBusinessUnit")).Attributes.Add("style", "text-align:right");
        //    ((TextBox)e.Row.Cells[3].FindControl("textBoxPosition")).Attributes.Add("style", "text-align:right");
        //    ((TextBox)e.Row.Cells[4].FindControl("textBoxMaxNoMember")).Attributes.Add("style",
        //                                                                               "background-color: #E1E1E1");
        //}

        //private void CreateSourceTable()
        //{
        //    DataTable dataTableAuthorizationGroupLevel = new DataTable();
        //    //dataTableAuthorizationGroupLevel.Columns.Add("LINENO", typeof(int));
        //    dataTableAuthorizationGroupLevel.Columns.Add("BUSINESSUNIT");
        //    dataTableAuthorizationGroupLevel.Columns.Add("BUSINESSUNITID");
        //    ////
        //    dataTableAuthorizationGroupLevel.Columns.Add("SELECTEDNODEBULIST");
        //    dataTableAuthorizationGroupLevel.Columns.Add("POSITION");
        //    dataTableAuthorizationGroupLevel.Columns.Add("CURRENTPOSITIONID");
        //    dataTableAuthorizationGroupLevel.Columns.Add("MAXNOOFMEMBERS");
        //    dataTableAuthorizationGroupLevel.Columns.Add("AuthorizationGroupLevelId");


        //    dataTableAuthorizationGroupLevel.Columns[0].AutoIncrement = true;
        //    dataTableAuthorizationGroupLevel.Columns[0].AutoIncrementSeed = 1;

        //    ViewState["griddatasource"] = dataTableAuthorizationGroupLevel;
        //    gridViewAuthorizationGroupLevel.Enabled = false;
        //}

        public void AddAuthorizationGroup()
        {
            try
            {
                if (Page.IsValid)
                {
                    AuthorizationGroup authorizationGroups = new AuthorizationGroup();
                    AuthorizationGroupService authorizationGroupsService = new AuthorizationGroupService();
                    OrganizationLevelListService organizationLevelListService = new OrganizationLevelListService();

                    //if (!(String.IsNullOrEmpty(Session[Global.KeyCurrentCompanyId].ToString())))
                    //{
                        //authorizationGroups.OrganizationLevelListId = organizationLevelListService.GetByIdOrganizationLevelList(Convert.ToInt64(Session[Global.KeyCurrentCompanyId]));
                        authorizationGroups.OrganizationLevelListId = 1;
                    //}
                    if (textBoxAuthorizationGroupCodeAdd.Text != String.Empty)
                    {
                        authorizationGroups.Code = textBoxAuthorizationGroupCodeAdd.Text.Trim();
                    }
                    if (textBoxAuthorizationGroupDescriptionAdd.Text != String.Empty)
                    {
                        authorizationGroups.Description = textBoxAuthorizationGroupDescriptionAdd.Text.Trim();
                    }

                    authorizationGroupsService.AddAuthorizationGroup(authorizationGroups);
                    Message("Successfully Saved", false);
                }
            }
            catch (Exception exception)
            {
                Message("Saving Failed", true);
            }

        }

        /// <summary>
        /// Display messages.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Message(string message, Boolean error)
        {
            if (error)
            {
                FlashMessage1.Display(message, "errormessage");

            }
            else
            {
                FlashMessage1.Display(message, "okmessage");

            }
        }

        public void ButtonSave_Click(object sender, EventArgs e)
        {
            if (CheckMandatoryAuthorizationRequirementAdd())
            {
                AddAuthorizationGroup();
                ClearAuthorizationGroupInputs();
                DataBindToGridViewAuthorization();
            }
        }

        public void ButtonAdd_Click(object sender, EventArgs e)
        { 
        
        }

        public void ButtonModify_Click(object sender, EventArgs e)
        {

        }

        public void ButtonDelete_Click(object sender, EventArgs e)
        {

        }

        public void ButtonClear_Click(object sender, EventArgs e)
        {

        }

        

        private Boolean CheckMandatoryAuthorizationRequirementAdd()
        {

            if (textBoxAuthorizationGroupCodeAdd.Text == String.Empty)
            {
                Message("Code cannot be blank", true);
                return false;

            }
            if (textBoxAuthorizationGroupDescriptionAdd.Text == String.Empty)
            {
                Message("Description cannot be blank", true);
                return false;

            }

            return true;
        }

        //private Boolean CheckMandatoryAuthorizationRequirementModify()
        //{

        //    if (textBoxAuthorizationGroupCodeModify.Text == String.Empty)
        //    {
        //        Message("Code cannot be blank", true);
        //        return false;

        //    }
        //    if (textBoxAuthorizationGroupDescriptionModify.Text == String.Empty)
        //    {
        //        Message("Description cannot be blank", true);
        //        return false;

        //    }

        //    return true;
        //}

        private void ClearAuthorizationGroupInputs()
        {
            textBoxAuthorizationGroupCodeAdd.Text = String.Empty;
            textBoxAuthorizationGroupDescriptionAdd.Text = String.Empty;
        }

        public void ModifyAuthorizationGroups()
        {
            try
            {
                if (Page.IsValid)
                {
                    AuthorizationGroup authorizationGroups = new AuthorizationGroup();
                    AuthorizationGroupService authorizationGroupsService = new AuthorizationGroupService();
                    OrganizationLevelListService organizationLevelListService = new OrganizationLevelListService();

                    if (textBoxAuthorizationGroupCodeAdd.Text != String.Empty)
                    {
                        authorizationGroups.AuthorizationGroupId = Convert.ToInt16(textBoxAuthorizationGroupCodeAdd.Text.Trim());
                    }
                    if (!(String.IsNullOrEmpty(Session[Global.KeyCurrentCompanyId].ToString())))
                    {
                        authorizationGroups.OrganizationLevelList = organizationLevelListService.GetByIdOrganizationLevelList(Convert.ToInt16(Session[Global.KeyCurrentCompanyId]));
                    }
                    if (textBoxAuthorizationGroupCodeAdd.Text != String.Empty)
                    {
                        authorizationGroups.Code = textBoxAuthorizationGroupCodeAdd.Text.Trim();
                    }
                    if (textBoxAuthorizationGroupDescriptionAdd.Text != String.Empty)
                    {
                        authorizationGroups.Description = textBoxAuthorizationGroupDescriptionAdd.Text.Trim();
                    }

                    authorizationGroupsService.ModifyAuthorizationGroup(authorizationGroups);
                    Message("Successfully Saved", false);
                }
            }
            catch (Exception exception)
            {
                Message("Saving Failed", true);
            }

        }

        public void buttonAuthorizationGroupModify_Click(object sender, EventArgs e)
        {
            //if (CheckMandatoryAuthorizationRequirementModify())
            //{
                ModifyAuthorizationGroups();
                ClearAuthorizationGroupInputs();
            //    DataBindToGridViewAuthorization();
            //}
        }

        public void buttonAuthorizationGroupDelete_Click(object sender, EventArgs e)
        {
            if (CheckMandatoryAuthorizationGroupDelete())
            {
                DeleteAuthorizationGroups();
                ClearAuthorizationGroupInputs();
                DataBindToGridViewAuthorization();
            }
        }

        private Boolean CheckMandatoryAuthorizationGroupDelete()
        {


            if (textBoxAuthorizationGroupCodeAdd.Text == String.Empty)
            {
                Message("AuthorizationGroupId cannot be blank", true);
                return false;

            }

            return true;
        }

        public void DeleteAuthorizationGroups()
        {
            try
            {
                if (Page.IsValid)
                {
                    AuthorizationGroupService authorizationGroupsService = new AuthorizationGroupService();

                    authorizationGroupsService.DeleteAuthorizationGroup(Convert.ToInt16(textBoxAuthorizationGroupCodeAdd.Text));
                    Message("Successfully Deleted", false);
                }
            }
            catch (Exception exception)
            {
                Message("Deleting Failed", true);
            }

        }

        //protected void GridViewAuthorizationGroupLevel__RowDeleting(Object sender, GridViewDeleteEventArgs e)
        //{
        //    DataTable dataTableDataSource = ((DataTable)ViewState["griddatasource"]);

        //    //if (dataTableDataSource.Rows[e.RowIndex]["GLMASTERID"] == null || dataTableDataSource.Rows[e.RowIndex]["GLMASTERID"].ToString() == String.Empty || dataTableDataSource.Rows[e.RowIndex]["GLMASTERID"].ToString() == "&nbsp;")
        //    //{
        //    dataTableDataSource.Rows.RemoveAt(e.RowIndex);
        //    ViewState["griddatasource"] = dataTableDataSource;
        //    gridViewAuthorizationGroupLevel.DataSource = dataTableDataSource;
        //    gridViewAuthorizationGroupLevel.DataBind();

        //    //UpdateBelowRecordBalance(e.RowIndex);
        //    //CalculateTotal();
        //    //}
        //}

    }
}